package com.woorigil.social.controller.api.socialshopping;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.woorigil.social.MobileAppType;
import com.woorigil.social.controller.BaseController;
import com.woorigil.social.domain.ApplicationService;
import com.woorigil.social.domain.deal.DealDao;
import com.woorigil.social.domain.deal.DealFindService;
import com.woorigil.social.domain.deal.DealList;
import com.woorigil.social.domain.deal.repository.DealListCondition;
import com.woorigil.social.domain.query.QueryService;

@Controller
@RequestMapping("/api/socialshopping/v20")
public class Version2_0_ApiController extends BaseController {
	@Autowired QueryService queryService;
	@Autowired DealFindService dealFindService;
	@Autowired ApplicationService applicationService;
	
	
	@RequestMapping("/infomation/all")
	public ModelAndView informations(String sid) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("appVersion", applicationService.getLastReleaseAppVersion(MobileAppType.SOCIAL_SHOPPING));
		mav.addObject("dataVersion", applicationService.getLastReleaseDataVersion(MobileAppType.SOCIAL_SHOPPING));
		mav.addObject("dealListLimitCount", DealListCondition.defaultLimit);
		mav.addObject("dealTotalCount", applicationService.getTotalItemCount());
		mav.addObject("lastUpdatedTime", applicationService.getLastUpdatedAt().getTime());
		mav.addObject("categories", applicationService.getCategories());
		mav.addObject("providers", applicationService.getProviders());
		return mav;
	}
	
	@RequestMapping("/deals/all")
	public ModelAndView allDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList categoryDealList = dealFindService.findCategoryDealList(sid, mainCategoryId, subCategoryId, offset);
		DealList newDealList = dealFindService.findNewDealList(sid, mainCategoryId, subCategoryId, offset);
		DealList endDealList = dealFindService.findEndDealList(sid, mainCategoryId, subCategoryId, offset);
		DealList freeDealList = dealFindService.findFreeDealList(sid, mainCategoryId, subCategoryId, offset);
		
		ModelAndView mav = new ModelAndView("");
		mav.addObject("categoryDeals", categoryDealList.getDeals());
		mav.addObject("newDeals", newDealList.getDeals());
		mav.addObject("endDeals", endDealList.getDeals());
		mav.addObject("freeDeals", freeDealList.getDeals());
		return mav;
	}		
	
	@RequestMapping("/deals/category")
	public ModelAndView categoryDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList categoryDealList = dealFindService.findCategoryDealList(sid, mainCategoryId, subCategoryId, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("categoryDeals", categoryDealList.getDeals());
		return mav;
	}
	
	@RequestMapping("/deals/new")
	public ModelAndView newDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList newDealList = dealFindService.findNewDealList(sid, mainCategoryId, subCategoryId, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("newDeals", newDealList.getDeals());
		return mav;
	}
	
	@RequestMapping("/deals/end")
	public ModelAndView endDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList endDealList = dealFindService.findEndDealList(sid, mainCategoryId, subCategoryId, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("endDeals", endDealList.getDeals());
		return mav;
	}
	
	@RequestMapping("/deals/free")
	public ModelAndView freeDeals(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealList freeDealList = dealFindService.findFreeDealList(sid, mainCategoryId, subCategoryId, offset);
		ModelAndView mav = new ModelAndView("");
		mav.addObject("freeDeals", freeDealList.getDeals());
		return mav;
	}		
	
	@RequestMapping("/deals/recent")
	public ModelAndView recentlyViewedItems(String sid, int offset) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("recentViewDeals", dealFindService.fidnViewedDealBox(sid, offset));
		return mav;
	}	
	
	@RequestMapping("/query/all")
	public ModelAndView recentQueries(String sid) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("queryBox", queryService.findAllBySid(sid));
		return mav;
	}
	
	@RequestMapping("/query/delete")
	public ModelAndView recentQueries(String sid, String query) {
		ModelAndView mav = new ModelAndView("");
		mav.addObject("count", queryService.deleteRecentQueryBySidAndQuery(sid, query));
		return mav;
	}

	@Autowired DealDao dealDao;
	@RequestMapping("/search")
	public ModelAndView search(String sid, String query, int offset) {
		ModelAndView mav = new ModelAndView("");
		DealList dealList = dealFindService.search(sid, query, offset);
		mav.addObject("deals", dealList.getDeals());
		return mav;
	}
}
